package com.ihome.android.market2.asynctask;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.ihome.android.academy.AppMgrActivity;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.academy.R;
import com.ihome.android.market2.view.DialogFactory;
import com.ihome.android.market2.view.MyProgressDialog;
import com.ihome.android.util.BgInstallApk;
import com.ihome.android.util.log.LogUtil;
import com.ihome.android.util.log.UserBehaviorLog;
import com.ihome.android.util.log.UserBehaviorLog.NotifyType;
import com.ihome.android.util.log.UserBehaviorLog.UserBehavior;

/**
 * 卸载应用
 * 
 * @author Emerson
 * 
 */
public class UninstallAppTask extends AsyncTask<Bundle, Integer, Bundle> {
	private final String		TAG	= UninstallAppTask.class.getSimpleName();
	private Context				context;
	private MyProgressDialog	progressDialog;
	private boolean				unInstallResult;
	private String				pkgName;

	public UninstallAppTask(Context context) {
		this.context = context;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progressDialog = MyProgressDialog.getDialog(context, context.getString(R.string.uninstalling_app));
		progressDialog.show();
		progressDialog.setCancelable(false);
		progressDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				if (UninstallAppTask.this.getStatus().compareTo(AsyncTask.Status.FINISHED) != 0)
					UninstallAppTask.this.cancel(true);
			}
		});
	}

	@Override
	protected Bundle doInBackground(Bundle... params) {
		Bundle b = params[0];
		pkgName = b.getString(HttpKeys.KEY_PACKAGENAME);
		if (TextUtils.isEmpty(pkgName)) {
			String[] msg1 = { "", "", "" , "", "UninstallApp", "" };
			UserBehaviorLog.getInstance().writeLog(UserBehavior.STOREUPDATE_EXCEPTION, NotifyType.TYPE_FAILURE, "", msg1);
			return null;
		}

		Object[] unInstallRst = BgInstallApk.uninstallAppInBackGround(pkgName);
		unInstallResult = ((Boolean)unInstallRst[0]).booleanValue();
		
		//unInstallResult = BgInstallApk.doUninstallationSliently(pkgName);
		LogUtil.v(TAG, "unInstallResult = " + unInstallResult);
		return b;
	}

	@Override
	protected void onPostExecute(Bundle result) {
		super.onPostExecute(result);
		if (progressDialog != null)
			progressDialog.dismiss();

		/** 安装失败处理 */
		if (!unInstallResult) {
			String[] msgx = {"","","","","UninstallApp",""};
			UserBehaviorLog.getInstance().writeLog(UserBehavior.APPDOWNLOAD_EXCEPTION, NotifyType.TYPE_FAILURE, "", msgx);
			new DialogFactory(context).showDialog(context.getString(R.string.app_uninstall_failure));
			return;
		}
		/** 安装成功处理 */
		else if (unInstallResult &&  context instanceof AppMgrActivity) {
			new DialogFactory(context).showToast(context.getString(R.string.app_uninstall_success), Toast.LENGTH_SHORT);
		}

	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
		LogUtil.i(TAG, "--> onCancelled");
		if (progressDialog != null)
			progressDialog.dismiss();

	}

}
